# -*- mode: python ; coding: utf-8 -*-
import sys
import os
from PyInstaller.building.build_main import Analysis, PYZ, EXE, COLLECT
from PyInstaller.utils.hooks import collect_all

# 收集resources/icon目录中的所有文件 - 打包到icon子目录

# 确保resources/icon目录存在
icon_dir = os.path.abspath('resources/icon')
if not os.path.exists(icon_dir):
    raise ValueError(f'Icon directory not found: {icon_dir}')

# 收集icon目录中的所有文件
icon_files = []
for file_name in os.listdir(icon_dir):
    file_path = os.path.join(icon_dir, file_name)
    if os.path.isfile(file_path):
        # 保持目录结构，打包到icon子目录
        # 使用通用路径格式，避免硬编码反斜杠
        icon_files.append((file_path, 'icon'))

# 添加额外的检查，确保icon_files不为空
if not icon_files:
    raise ValueError(f'No icon files found in the icon directory: {icon_dir}')

# 打印收集到的图标文件，这将在PyInstaller的日志中显示
# print(f"Collected icon files: {icon_files}")

block_cipher = None
current_dir = os.getcwd()

# 收集依赖
ttkb_binaries, ttkb_datas, ttkb_hiddenimports = collect_all('ttkbootstrap')
tkdnd_binaries, tkdnd_datas, tkdnd_hiddenimports = collect_all('tkinterdnd2')
tks_binaries, tks_datas, tks_hiddenimports = collect_all('tksheet')

# 合并依赖
all_binaries = ttkb_binaries + tkdnd_binaries + tks_binaries
all_datas = ttkb_datas + tkdnd_datas + tks_datas
all_hiddenimports = (ttkb_hiddenimports + tkdnd_hiddenimports + 
                     tks_hiddenimports + ['tkinter', 'tkinter.ttk', 
                     'sqlite3', 'PIL', 'PIL.Image', 'PIL.ImageTk'])

# 主Analysis配置
a = Analysis(
    ['src/main.py'],
    pathex=[current_dir, os.path.join(current_dir, 'src')],
    binaries=all_binaries,
    datas=[
        (os.path.join(current_dir, 'README.md'), '.')  # 将README.md添加到根目录
    ] + icon_files + all_datas,  # 包含README.md、图标文件和其他依赖数据，不包含py源文件
             hiddenimports=all_hiddenimports + ['dao', 'models', 'services', 'views', 'utils', 'configs'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

# 创建EXE对象
exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],  # options
    name='业余无线电模拟考试系统',
    debug=False,
     bootloader_ignore_signals=False,
     strip=False,
     upx=True,
     upx_exclude=[],
     runtime_tmpdir=None,
     console=False,
     disable_windowed_traceback=False,
     argv_emulation=False,
     target_arch=None,
     codesign_identity=None,
     entitlements_file=None,
     icon=os.path.join(current_dir, 'resources', 'icon', 'icon.ico')
 )